#include "games/colorgame.h"

ColorGame::ColorGame(QWidget *parent, int difflvl) :
    BaseGame(parent, difflvl)
{
    obtained_color_label = new QLabel();
    requested_color_label = new QLabel();
    obtained_color_label->setMinimumSize(50,50);
    requested_color_label->setMinimumSize(50,50);

    sliderR = new QSlider();
    sliderG = new QSlider();
    sliderB = new QSlider();
    sliderR->setMinimum(0);
    sliderG->setMinimum(0);
    sliderB->setMinimum(0);
    sliderR->setMaximum(255);
    sliderG->setMaximum(255);
    sliderB->setMaximum(255);
    sliderR->setTickInterval(1);
    sliderG->setTickInterval(1);
    sliderB->setTickInterval(1);
    sliderR->setSingleStep(1);
    sliderG->setSingleStep(1);
    sliderB->setSingleStep(1);
    sliderR->setPageStep(10);
    sliderG->setPageStep(10);
    sliderB->setPageStep(10);

    lay->addWidget(obtained_color_label,0,0,1,3,Qt::AlignHCenter);
    lay->addWidget(requested_color_label,0,3,1,3,Qt::AlignHCenter);
    lay->addWidget(sliderR,1,0,1,2,Qt::AlignHCenter);
    lay->addWidget(sliderG,1,2,1,2,Qt::AlignHCenter);
    lay->addWidget(sliderB,1,4,1,2,Qt::AlignHCenter);

    connect(sliderR,SIGNAL(valueChanged(int)),this,SLOT(onColorChanged()));
    connect(sliderG,SIGNAL(valueChanged(int)),this,SLOT(onColorChanged()));
    connect(sliderB,SIGNAL(valueChanged(int)),this,SLOT(onColorChanged()));

    requested_color = QColor(qrand() % 255, qrand() % 255, qrand() % 255);
    QString str = QString("background-color: %1;");
    requested_color_label->setStyleSheet(str.arg(requested_color.name()));

    sliderR->setValue(qrand() % 255);
    sliderG->setValue(qrand() % 255);
    sliderB->setValue(qrand() % 255);
}

QString ColorGame::hint()
{
    int diff_R = obtained_color.red() - requested_color.red();
    int diff_G = obtained_color.green() - requested_color.green();
    int diff_B = obtained_color.blue() - requested_color.blue();
    if( qAbs(diff_R) < 32 - 2 * difficulty_level &&
        qAbs(diff_G) < 32 - 2 * difficulty_level &&
        qAbs(diff_B) < 32 - 2 * difficulty_level ) {
        return QString("(%1,%2,%3)").arg(diff_R).arg(diff_G).arg(diff_B);
    }
    return QString("You are far far away.");
}

void ColorGame::onColorChanged()
{
    obtained_color = QColor(sliderR->value(), sliderG->value(), sliderB->value());
    QString str = QString("background-color: %1;");
    obtained_color_label->setStyleSheet(str.arg(obtained_color.name()));
}
